﻿using UnityEngine;
using System.Collections;

public class AnimationControlDemo : MonoBehaviour {

	private MecanimControl mecanimControl;
	private bool mirror;
	private bool rotate = false;

	// Use this for initialization
	void Start () {
		mecanimControl = gameObject.GetComponent<MecanimControl>();

		// Possible Events:
		MecanimControl.OnAnimationBegin += OnAnimationBegin;
		MecanimControl.OnAnimationEnd += OnAnimationEnd;
		MecanimControl.OnAnimationLoop += OnAnimationLoop;
	}

	// Fires whenever a new animation begins
	void OnAnimationBegin(AnimationData animData){
		Debug.Log(animData.clipName + " has started.");
	}
	
	// Fires whenever an animation ends
	void OnAnimationEnd(AnimationData animData){
		Debug.Log(animData.clipName + " has ended.");
	}
	
	// Fires whenever an animation loops
	void OnAnimationLoop(AnimationData animData){
		Debug.Log(animData.clipName + " has looped "+ animData.timesPlayed + " times.");
	}
	
	// Update is called once per frame
	void Update () {
		if (rotate) transform.Rotate(0, 20 * Time.deltaTime, 0);
	}

	void OnGUI(){
		if (GUILayout.Button("Invert Speed")) mecanimControl.SetSpeed(-mecanimControl.GetSpeed());
		if (GUILayout.Button("Rotate "+ rotate)) rotate = !rotate;
		if (GUILayout.Button("Mirror "+ mirror)) {
			mirror = !mirror;
			mecanimControl.SetMirror(mirror);
		}

		GUILayout.Space(10);

		foreach(AnimationData animationData in mecanimControl.animations){
			if (GUILayout.Button(animationData.clipName)){
				mecanimControl.Play(animationData, mirror);
			}
		}
	}
}
